import {
  Body,
  Controller,
  Post,
  Headers,
  UseGuards,
  UsePipes,
  ValidationPipe,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { AuthGuard } from '@nestjs/passport';
import {
  ApiBasicAuth,
  ApiOperation,
  ApiSecurity,
  ApiTags,
} from '@nestjs/swagger';
import { RpAccount } from 'src/logical/account/entity/account.entity';
import { AccountService } from 'src/logical/account/service/account.service';
import { BaseResultModel } from 'src/logical/common/models/BaseResult.model';
import { RpOrgEntity } from 'src/logical/org/entity/org.entity';
import { RoleEntity } from 'src/logical/role/entity/role.entity';
import { RoleService } from 'src/logical/role/service/role.service';
import { loginAccountModel } from '../models/loginAccount.model';
import { ResetPwdModel } from '../models/resetPwd.model';
import { TokenInfoModel } from '../models/tokenInfo.model';
import { UserInfoModel } from '../models/userInfo.model';
import { AuthService } from '../service/auth.service';
@ApiSecurity('Authorization')
@ApiTags('登录验证相关接口')
@Controller('/api/auth')
export class AuthController {
  constructor(
    private readonly authService: AuthService,
    private readonly accountService: AccountService,
    private readonly roleService: RoleService,
    private readonly jwtService: JwtService,
  ) {}

  @UseGuards(AuthGuard('jwt'))
  @ApiOperation({ summary: '获取用户信息' })
  @Post('getUserInfo')
  async getUserInfo(
    @Headers('authorization') token,
  ): Promise<BaseResultModel<object>> {
    const oldToken = token;
    const tokenInfoModel: TokenInfoModel = JSON.parse(
      JSON.stringify(this.jwtService.decode(oldToken)),
    );
    const userInfoModel = new UserInfoModel();
    userInfoModel.accountId = tokenInfoModel.accountId;
    userInfoModel.name = tokenInfoModel.username;
    userInfoModel.orgId = tokenInfoModel.orgId;
    userInfoModel.orgName = tokenInfoModel.orgName;
    userInfoModel.level = tokenInfoModel.level;
    userInfoModel.orgType = tokenInfoModel.orgType;
    userInfoModel.avatar = 'avatar';
    userInfoModel.introduction = 'introduction';

    let roles = (
      await this.roleService.findRoleIdByAccountId(tokenInfoModel.accountId)
    ).map((item) => {
      return item.get('roleId');
    });

    userInfoModel.roles = roles;

    const user = await RpAccount.findByPk(tokenInfoModel.accountId);

    userInfoModel.oneOrgId = user.get().oneOrgId;
    userInfoModel.oneOrgName = user.get().oneOrgName;
    userInfoModel.twoOrgId = user.get().twoOrgId;
    userInfoModel.twoOrgName = user.get().twoOrgName;
    userInfoModel.threeOrgId = user.get().threeOrgId;
    userInfoModel.threeOrgName = user.get().threeOrgName;

    return this.authService.resultData(userInfoModel, 200, '');
  }

  @UsePipes(new ValidationPipe())
  @ApiOperation({ summary: '管理端登录入口' })
  @Post('loginByAccount')
  async loginByAccount(@Body() body: loginAccountModel) {
    return this.authService.loginByAccount(body);
  }

  @UseGuards(AuthGuard('jwt'))
  @ApiOperation({ summary: '用户登出' })
  @Post('logout')
  async logout(): Promise<BaseResultModel<string>> {
    return this.authService.resultData('', 200, '登出成功');
  }

  @UseGuards(AuthGuard('jwt'))
  @ApiOperation({ summary: '修改密码' })
  @Post('updatePwd')
  async updatePwd(
    @Headers('authorization') token,
    @Body() inputModel: ResetPwdModel,
  ) {
    const tokenInfoModel: TokenInfoModel = JSON.parse(
      JSON.stringify(this.jwtService.decode(token)),
    );

    return this.accountService.updatePwd(tokenInfoModel, inputModel);
  }
}
